import { defineStore } from 'pinia'

export const useRegisterStore = defineStore('register', () => {
  const registerTypeList = [
    {
      index: 1,
      code: 'coilRegister',
      name: '线圈寄存器'
    },
    {
      index: 2,
      code: 'discreteInputRegister',
      name: '离散输入寄存器'
    },
    {
      index: 3,
      code: 'holdingRegister',
      name: '保持寄存器'
    },
    {
      index: 4,
      code: 'inputRegister',
      name: '输入寄存器'
    },
  ]

  const formatHex = (value: number | string, withPrefix: boolean = true) => {
    const num = typeof value === 'string' ? parseInt(value) : value;
    const hexStr = num.toString(16).toUpperCase().padStart(4, '0');
    return withPrefix ? `0x${hexStr}` : hexStr;
  };

  return { registerTypeList, formatHex }
})
